package com.vanvalt.payhui.modules.pay.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.vanvalt.payhui.modules.pay.entity.Feedback;
import com.vanvalt.payhui.modules.pay.service.FeedbackService;

/**
 * @author Xiaoyang
 * 意见反馈
 */
@Controller
@RequestMapping(value = "${adminPath}/pay/feedback")
public class FeedbackController extends BaseController {

	@Autowired
	private FeedbackService feedbackService;
	
	@ModelAttribute
	public Feedback get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return feedbackService.get(id);
		}else{
			return new Feedback();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Feedback feedback, HttpServletRequest request, HttpServletResponse response, Model model) {

        Page<Feedback> page = feedbackService.findPage(new Page<Feedback>(request, response), feedback); 
        model.addAttribute("page", page);
		return "modules/pay/feedbackList";
	}
	
	@RequestMapping(value = "form")
	public String form(Feedback feedback, Model model) {
		
		model.addAttribute("feedback", feedback);
		return "modules/pay/feedbackForm";
	}
	
	@RequestMapping(value = "save")
	public String save(Feedback feedback, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, feedback)){
			return form(feedback, model);
		}
		User user = UserUtils.getUser();
		feedback.setUserId(user.getId());
		
		feedbackService.save(feedback);
		addMessage(redirectAttributes, "保存意见反馈信息'" + StringUtils.abbr(feedback.getTitle(),50) + "'成功");
		return "redirect:" + adminPath + "/pay/feedback/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Feedback feedback, @RequestParam(required=false) Boolean isRe, RedirectAttributes redirectAttributes) {
		
		feedbackService.delete(feedback);
		addMessage(redirectAttributes, (isRe!=null&&isRe?"发布":"删除")+"意见反馈信息成功");
		return "redirect:" + adminPath + "/pay/feedback/?repage";
	}
	
}
